// pages/person/index.js
const api = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopObj:{}
    ,uploaderList: [],
    uploaderNum:0,
    showUpload:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if( options.uid || wx.getStorageSync('uid')){
      this.setData({
          isShare:true
      })
      let uid = options.uid || wx.getStorageSync('uid')
      wx.setStorageSync('uid', uid)
      // 业务员分享分享进入
      let obj = {
          opcode:"Int16Array",
          id: parseInt(uid)
      }
      api.userinfo({
          data:obj,
          method:'post',
          success: (res)=>{
              this.setData({
                  'shopObj.logo':res.data.data.data[0].imgpath,
                  'shopObj.name':res.data.data.data[0].name,
                  'shopObj.phone':res.data.data.data[0].phone,
                  'shopObj.user':res.data.data.data[0].user,
                  'shopObj.permission':res.data.data.data[0].permission,
                  'shopObj.job':res.data.data.data[0].job,
                  uploaderList:[res.data.data.data[0].imgpath]
              })
              
          }
      })

  }else{
      console.log('noshare')
  }
  },
  // 删除图片
  clearImg:function(e){
    var nowList = [];//新数据
    var uploaderList = this.data.uploaderList;//原数据
    
    for (let i = 0; i < uploaderList.length;i++){
        if (i == e.currentTarget.dataset.index){
            continue;
        }else{
            nowList.push(uploaderList[i])
        }
    }
    this.setData({
        uploaderNum: this.data.uploaderNum - 1,
        uploaderList: nowList,
        showUpload: true
    })
},
//展示图片
showImg:function(e){
    var that=this;
    wx.previewImage({
        urls: that.data.uploaderList,
        current: that.data.uploaderList[e.currentTarget.dataset.index]
    })
},
//上传图片
upload: function(e) {
    var that = this;


    wx.chooseImage({
        count: 9 - that.data.uploaderNum, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
           var nums  =0;
    var  getFiles= ()=> {
        wx.getFileInfo({
          filePath: res.tempFilePaths[0],
          success(files){
              if(files.errMsg == "getFileInfo:ok"){
                  wx.uploadFile({
                    filePath:res.tempFilePaths[0],
                    name: 'filePath',
                    header: {
                        Accept: 'application/json',
                        'Content-Type': 'application/json',
                        authorization: wx.getStorageSync('token')
                      },
                    url: 'https://www.doudouxny.com/updateimg',
                    complete: function(files){
                        if( nums <5){
                            nums+=1;
                            if(files.errMsg="uploadFile:ok"){
                                let tempFilePaths = JSON.parse(files.data).data;
                                let uploaderList = that.data.uploaderList.concat(tempFilePaths);
                                if (uploaderList.length==1){
                                    that.setData({
                                        showUpload:false
                                    })
                                }
                                that.setData({
                                    uploaderList: uploaderList,
                                    uploaderNum: uploaderList.length,
                                })
                                wx.showToast({
                                    title: '上传成功',
                                  })
                            }else{
                                getFiles();
                            }
                        }
                    }
                  })
              }
          }
        })
    }
    getFiles()
    // debugger
    //         if(res.errMsg=="chooseImage:ok"){
    //             wx.uploadFile({
    //                 filePath:res.tempFilePath,
    //                 name: 'name',
    //                 url: 'https://www.doudouxny.com/updateimg',
    //                 complete: function(res){
    //                     console.log(res,'aa')
    //                 }
    //             })
    //         }

            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片

        }
    })
},
  onChange(e){
    this.setData({
      'shopObj.name':e.detail
  })
  },
  onChange2(e){
    this.setData({
      'shopObj.phone':e.detail
  })
  },
  handleSubmit(){
        let uid = wx.getStorageSync('uid')
        api.userchange({
            data: {
              id: parseInt(uid),
              imgpath:this.data.uploaderList[0],
              name:this.data.shopObj.name,
              phone:this.data.shopObj.phone,
              job:this.data.shopObj.job,
              user:this.data.shopObj.user,
              permission:this.data.shopObj.permission
            },
            method:'post',
            success(res){
                console.log(res)
                if(res.data.success==true){
                    wx.showToast({
                      title: '修改成功',
                    })
                }
            }
        })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onClickHide() {
    wx.navigateBack()
 },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})